/*
 * @Author: huangtianyang 916072572@qq.com
 * @Date: 2023-09-01 09:15:51
 * @LastEditors: huangtianyang
 * @LastEditTime: 2023-10-07 18:09:46
 * @FilePath: \cxl-admin\src\pages\operation\setMeal\setMealDialog\index.ts
 */
import { setMealAdd, setMealDetailedById, setMealInterface, setMealUpdate } from '@/apis/operation/setMealPrice'
import { ElMessage, FormInstance } from 'element-plus'
import { reactive } from 'vue'
import { packageNameMap, productTypeMap } from '@/utils/config'

export class SetMealPrice {
    //价格配置
    config = reactive({
        type: 'create',
        productType: '',
        productTypeNumber: '',
        packageName: '',
        packageNameNumber: 0,
        marketPrice: '',
        preferentialPrice: '',
        salesPrice: '',
        count: 1,
        price: '',
        limitCount: '-1',
        sort: '',
        label: '',
        status: true,
        id: ''
    })
    //时效性选项
    expireOptions = reactive([
        { label: '长期有效', value: '-1' },
        { label: '15天', value: '2' },
        { label: '30天', value: '3' },
        { label: '60天', value: '4' }
    ])
    validity = ['长期有效', '15天', '30天', '60天']

    //表单校验
    configRules = reactive({
        productType: [{ required: true, message: '请选择产品类型', trigger: 'change' }],
        packageName: [{ required: true, message: '请选择套餐名称', trigger: 'change' }],
        marketPrice: [{ required: true, message: '请输入市场价', trigger: 'blur' }],
        preferentialPrice: [{ required: false, message: '请输入优惠价', trigger: 'blur' }],
        count: [{ required: true, message: '请输入购买次数', trigger: 'blur' }],
        sort: [{ required: true, message: '请输入排序', trigger: 'blur' }]
    })

    //查询套餐详情
    getDetailedById(id: string) {
        setMealDetailedById(id).then(res => {
            if (res.data.value === 200) {
                this.config.productType = productTypeMap.get(res.data.data.productType + '')
                this.config.productTypeNumber = res.data.data.productType
                this.config.packageName = packageNameMap.get(res.data.data.packageNameNumber + '')
                this.config.packageNameNumber = res.data.data.packageNameNumber
                this.config.marketPrice = res.data.data.marketAmount
                this.config.preferentialPrice = (res.data.data.marketAmount - res.data.data.amount).toFixed(2)
                this.config.count = res.data.data.packageCount
                this.config.limitCount = res.data.data.limitCount
                this.config.sort = res.data.data.sort
                this.config.label = res.data.data.packageTag
                this.config.status = res.data.data.status ? true : false
            }
        })
    }

    /**
     * 确定编辑
     * @param formEl
     */
    onComfirmEdit = async (formEl: FormInstance | undefined, callback: () => void) => {
        if (!formEl) return
        await formEl.validate((valid, fields) => {
            if (valid) {
                console.log(this.config)
                const obj: setMealInterface = {
                    amount: (Number(this.config.marketPrice) - Number(this.config.preferentialPrice)).toFixed(2),
                    limitCount: Number(this.config.limitCount) || -1,
                    marketAmount: this.config.marketPrice,
                    packageCount: this.config.count,
                    packageName: packageNameMap.get(this.config.packageNameNumber),
                    packageTag: this.config.label,
                    packageNameNumber: Number(this.config.packageNameNumber),
                    productType: Number(this.config.productTypeNumber),
                    sort: Number(this.config.sort),
                    status: Number(this.config.status)
                }
                if (this.config.type === 'create') {
                    setMealAdd(obj).then(res => {
                        if (res.data.value === 200) {
                            callback()
                        } else {
                            ElMessage.error(res.data.desc)
                        }
                    })
                } else if (this.config.type === 'edit') {
                    obj.id = this.config.id
                    setMealUpdate(obj).then(res => {
                        if (res.data.value === 200) {
                            callback()
                        } else {
                            ElMessage.error(res.data.desc)
                        }
                    })
                }
            }
        })
    }
    //监听选择的产品类型
    onSelectProductType = (res: any) => {
        console.log(res)
        this.config.productTypeNumber = res
    }
    //监听选择的套餐名称
    onSelectPackageName = (res: any) => {
        console.log(res)
        this.config.packageNameNumber = res
    }
}
